package net.obxlabs.tweetpad;

import java.awt.*;
import net.nexttext.*;
import processing.core.*;

public class Blob implements Locatable {
	public static final int NUM_PTS = 30;
	public static final float CHANGE = 0.5f;
	public static final float SCALE_SPEED = 200.0f;  // greater = slower
	public static final float ROT_SPEED = 1000.0f;  // greater = slower
	
	private PApplet p;
	private Vector3 center;
	private int radius;
	private Rectangle bounds;
	
	public Blob(PApplet p, int x, int y, int r) {
		this.p = p;
		center = new Vector3(x, y);
		radius = r;
		bounds = new Rectangle((int)center.x-radius, (int)center.y-radius, radius*2, radius*2);
	}
	
	public void draw() {
		p.stroke(153, 204, 102);
		p.strokeWeight(2);
		p.fill(153, 204, 102, 100);
		
		p.pushMatrix();
			p.translate((float)center.x, (float)center.y);
			p.rotate((p.frameCount/ROT_SPEED));
			p.translate(-(float)center.x, -(float)center.y);
		  
			p.beginShape();
			for(int i=0; i < NUM_PTS+3; i++) {
				p.curveVertex((float)computeX(i%NUM_PTS), (float)computeY(i%NUM_PTS));
			}
			p.endShape();
		p.popMatrix();
		
		p.strokeWeight(1);
	}
	
	private double computeX(int i) {
		return (radius*(p.noise(p.frameCount/SCALE_SPEED, i)+CHANGE)*PApplet.sin(2*PApplet.PI*i/NUM_PTS)+center.x);
	}
	private double computeY(int i) {
		return (radius*(p.noise(p.frameCount/SCALE_SPEED, i)+CHANGE)*PApplet.cos(2*PApplet.PI*i/NUM_PTS)+center.y);
	}
	
	public Rectangle getBounds() { return bounds; }
	public Vector3 getLocation() { return center; }
}
